package com.example.demo.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.example.demo.dao.GridMemberDao;
import com.example.demo.pojo.GridMember;
import com.example.demo.vos.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Primary
public class GridMemberServiceImpl implements IGridMemberService {

    @Autowired
    private GridMemberDao dao;

    @Override
    public ResultVo getGmMembers() {
        // 实现获取所有网格员的方法
        List<GridMember> members = dao.selectList(null);
        return ResultVo.success(members);
    }

    @Override
    public ResultVo getOne(String gmCode, String password) {
        QueryWrapper<GridMember> qw = new QueryWrapper<>();
        qw.eq("gm_code", gmCode)
                .eq("password", password); // 修正了字段名

        GridMember gridMember = dao.selectOne(qw);

        if (gridMember == null) {
            return ResultVo.error("用户信息有误，不能登录");
        }
        return ResultVo.success(gridMember);
    }

    @Override
    public ResultVo getGmMembersByProvinceId(Integer provinceId) {
        QueryWrapper<GridMember> qw = new QueryWrapper<>();
        qw.eq("province_id", provinceId);

        List<GridMember> gridMembers = dao.selectList(qw);
        return ResultVo.success(gridMembers);
    }

    @Override
    public ResultVo getGridMemberByCodeByPass(GridMember gridMember) {
        GridMember one = dao.getGridMemberByCodeByPass(gridMember.getGmCode(), gridMember.getPassword());
        if (one == null) {
            return ResultVo.error("用户名或密码错误");
        }
        return ResultVo.success(one);
    }



    @Override
    public ResultVo listGridMemberByProvinceCityState(GridMember gridMember) {
        QueryWrapper<GridMember> qw = new QueryWrapper<>();
        if (gridMember.getProvinceId() != null) {
            qw.eq("province_id", gridMember.getProvinceId());
        }
        if (gridMember.getCityId() != null) {
            qw.eq("city_id", gridMember.getCityId());
        }
        if (gridMember.getState() != null) {
            qw.eq("state", gridMember.getState());
        }
        List<GridMember> list = dao.selectList(qw);
        return ResultVo.success(list);
    }

}